#include "structtest.h"

int structtest()
{
  int r;
  printf("TEST STRUKTUR DANYCH\n");
  r = testTuple();
  if (r != 0) return r;
  r = testPattern();
  if (r != 0) return r;
  r = testTupleA();
  if (r != 0) return r;
  r = testPatternA();
  if (r != 0) return r;
  
  return 0;
}

int testTuple()
{
  element_of_tuple e[3], *i;
  tuple* t;
  char str[] = "abcd";
  printf("Test tworzenia krotki\n");
  
  e[0].type = STRING;
  e[0].data._string = str;
  
  t = create_tuple(1, e[0]);  
  
  e[1].type = INTEGER;
  e[1].data._integer = 123;
  e[2].type = FLOAT;
  e[2].data._float = 3.14;
  
  add_elem_to_tuple(t, e[1]);
  add_elem_to_tuple(t, e[2]);
  i = t->first;
  if (i->type != STRING || i->data._string != str)
  {
    printf("Blad w elemencie krotki 1\n");
    return 1;
  }
  i = i->next;
  if (i->type != INTEGER || i->data._integer != 123)
  {
    printf("Blad w elemencie krotki 2\n");
    return 1;
  }
  i = i->next;
  if (i->type != FLOAT || i->data._float < 3.14)
  {
    printf("Blad w elemencie krotki 3\n");
    return 1;
  }
  i = i->next;
  if (i != NULL)
  {
    printf("Lista w krotce nie zakonczona NULLem\n");
    return 1;
  }
  delete_tuple(t);
  return 0;
}

int testPattern()
{
  element_of_pattern e[3], *i;
  pattern* p;
  char str[] = "def";
  
  printf("Test tworzenia wzorca\n");
  
  e[0].type = STRING;
  e[0].comp_oper = '=';
  e[0].data._string = str;
  p = create_pattern(1, e[0]);
  
  e[1].type = INTEGER;
  e[1].comp_oper = '>';
  e[1].data._integer = 897;
  
  add_elem_to_pattern(p, e[1]);
  
  i = p->first;
  if (i->type != STRING || i->data._string != str || i->comp_oper != '=')
  {
    printf("Blad w elemencie wzorca 1\n");
    return 1;
  }
  i = i->next;
  if (i->type != INTEGER || i->data._integer != 897 || i->comp_oper != '>')
  {
    printf("Blad w elemencie wzorca 2\n");
    return 1;
  }
  i = i->next;
  if (i != NULL)
  {
    printf("Lista w krotce nie zakonczona NULLem\n");
    return 1;
  }
  
  delete_pattern(p);
  
  return 0;
}

int testTupleA()
{
  element_of_tuple e[3], *i;
  tuple* t;
  char str[] = "abcd";
  printf("Test tworzenia krotki bez poczatkowej zawartosci\n");
  
  t = create_tuple(0);  
  
  e[0].type = STRING;
  e[0].data._string = str;
  e[1].type = INTEGER;
  e[1].data._integer = 123;
  e[2].type = FLOAT;
  e[2].data._float = 3.14;
  
  add_elem_to_tuple(t, e[0]);
  add_elem_to_tuple(t, e[1]);
  add_elem_to_tuple(t, e[2]);
  i = t->first;
  if (i->type != STRING || i->data._string != str)
  {
    printf("Blad w elemencie krotki 1\n");
    return 1;
  }
  i = i->next;
  if (i->type != INTEGER || i->data._integer != 123)
  {
    printf("Blad w elemencie krotki 2\n");
    return 1;
  }
  i = i->next;
  if (i->type != FLOAT || i->data._float < 3.14)
  {
    printf("Blad w elemencie krotki 3\n");
    return 1;
  }
  i = i->next;
  if (i != NULL)
  {
    printf("Lista w krotce nie zakonczona NULLem\n");
    return 1;
  }
  delete_tuple(t);
  return 0;
}

int testPatternA()
{
  element_of_pattern e[3], *i;
  pattern* p;
  char str[] = "def";
  
  printf("Test tworzenia wzorca bez poczatkowej zawartosci\n");
  
  p = create_pattern(0);
  
  e[0].type = STRING;
  e[0].comp_oper = '=';
  e[0].data._string = str;
  
  e[1].type = INTEGER;
  e[1].comp_oper = '>';
  e[1].data._integer = 897;
  
  add_elem_to_pattern(p, e[0]);
  add_elem_to_pattern(p, e[1]);
  
  i = p->first;
  if (i->type != STRING || i->data._string != str || i->comp_oper != '=')
  {
    printf("Blad w elemencie wzorca 1\n");
    return 1;
  }
  i = i->next;
  if (i->type != INTEGER || i->data._integer != 897 || i->comp_oper != '>')
  {
    printf("Blad w elemencie wzorca 2\n");
    return 1;
  }
  i = i->next;
  if (i != NULL)
  {
    printf("Lista w krotce nie zakonczona NULLem\n");
    return 1;
  }
  
  delete_pattern(p);
  
  return 0;
}
